/**
 * @file models/overview.js
 * @author
 */
import {routerRedux} from 'dva/router';
import api from '../api';
import Mock from 'mockjs'
import moment from 'moment'

export default {
	namespace : 'overview',
	state : {
		lPortId:'',
		timeRange:'0',
		cBenchCode:'',
		beginDate:'',
		endDate:'',
		isShow:true,
		pdtsearchId:'',//产品模糊搜索id
		onePdt:'',
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/investmentSummary/investDashboard') {
					dispatch({type: 'whetherRequest', payload: query, pathname})
					dispatch({
						type:'getListPdt',
						payload:{
							'productType':['LICAI','GONGMU','MONI','SIMU']
						}
					})
					dispatch({
						type:'getBListPdt',
					})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
			payload,
			pathname
		}, {put, select}) {
			const {lPortId, timeRange, cBenchCode, beginDate, endDate} = payload
			if (lPortId || timeRange || cBenchCode || beginDate || endDate) {
				if (!(lPortId && cBenchCode && ((timeRange == '0' && beginDate && endDate) || (timeRange != '0')))) {
					yield put({type: 'changeRouter', payload: {
							pathname
						}})
				}else{
					yield put({
						type:'setState',
						payload
					})
				}
			}
		},
		*getBListPdt({ payload:query }, { call, put }) {
        const response = yield call(api.benchmarkMgt.getBenchmark, query);
        const productList = response.data.map((item, i) => ({
          id: item.benchId + '',
          value: `${item.benchCode}：${item.benchName}`
        }))
        yield put({
          type: 'getBListPdtSuccess',
          payload:productList,
        });
      },
		* getListPdt({ payload:query }, { call, put }) {
        const response = yield call(api.getBrinsonPdt, query);
        const productList = response.data.map((item, i) => ({
          id: item.productId + '',
          value: `${item.productCode}：${item.productName}`
        }))
        
        yield put({
          type: 'getListPdtSuccess',
          payload:productList,
        });
        // yield put({
        //   type: 'getBListPdtSuccess',
        //   payload:productList,
        // });
      },
    * getOnePdt({payload},{call,put}){
      const pdtId = payload.pdtId
      const response = yield call(api.getOnePdt,pdtId);
      yield put({
        type: 'getOnePdtSuccess',
        payload: response
      });
    },
		* changeRouter({
			payload
		}, {put}) {
			yield put(routerRedux.push(payload))
		}

	},
	reducers : {
		setState(state, {payload}) {
			// console.log('payload',payload)
			return {
				...state,
				...payload
			}
		},
		clearState(state) {
			return {
				lPortId:'',
				timeRange:'0',
				cBenchCode:'',
				beginDate:'',
				endDate:''
			}
		},
		getBListPdtSuccess(state,{payload}){
    	const data = payload;
      console.log('getListPdtSuccess>1111',data)
    	data.map((item,i)=>{
      	if(item.benchId == 0){
      		data.splice(i, 1)
      	}
      })
      
      return {
        ...state,
        BListPdt:data
      }
    },
		getListPdtSuccess(state,{payload}){
      const data = payload;
      // console.log('getListPdtSuccess>1333',data)
      // data.map((item,i)=>{
      // 	if(item.id == '0'){
      // 		data.splice(i, 1)
      // 	}
      // })
      // data.map((item,i)=>{
      // 	if(item.id == '1'){
      // 		data.splice(i, 1)
      // 	}
      // })
      // data.map((item,i)=>{
      // 	if(item.id == '3'){
      // 		data.splice(i, 1)
      // 	}
      // })
      // data.map((item,i)=>{
      // 	if(item.id == '4'){
      // 		data.splice(i, 1)
      // 	}
      // })
      // console.log('productList>>11',data)
      return {
        ...state,
        ListPdt:data
      }
    },
    getOnePdtSuccess(state,{payload}){
    	const data = payload.data
    	const pdtN = data?`${data.productCode}:${data.productName}`:null;
    	return {
    		...state,
    		onePdt:pdtN
    	}
    },
	}
};
